package org.jeecg.modules.demo.Shenhefankuibiao.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.Shenhefankuibiao.entity.PeihebumenDto;
import org.jeecg.modules.demo.Shenhefankuibiao.entity.ShenhefankuiDto;
import org.jeecg.modules.demo.Shenhefankuibiao.entity.Shenhefankuibiao;
import org.jeecg.modules.demo.Shenhefankuibiao.service.IShenhefankuibiaoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.peihebumenbiao.entity.JieshouDto;
import org.jeecg.modules.demo.peihebumenbiao.entity.Peihebumenbiao;
import org.jeecg.modules.demo.peihebumenbiao.service.IPeihebumenbiaoService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import java.util.ArrayList;
import java.util.List;
 /**
 * @Description: 审核反馈表
 * @Author: jeecg-boot
 * @Date:   2025-10-09
 * @Version: V1.0
 */
@Tag(name="审核反馈表")
@RestController
@RequestMapping("/Shenhefankuibiao/shenhefankuibiao")
@Slf4j
public class ShenhefankuibiaoController extends JeecgController<Shenhefankuibiao, IShenhefankuibiaoService> {
	@Autowired
	private IShenhefankuibiaoService shenhefankuibiaoService;
	@Autowired
	private IPeihebumenbiaoService peihebumenbiaoService;
	/**
	 * 分页列表查询
	 *
	 * @param shenhefankuibiao
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "审核反馈表-分页列表查询")
	@Operation(summary="审核反馈表-分页列表查询")
	@PostMapping(value = "/list")
	public Result<IPage<ShenhefankuiDto>> queryPageList(@RequestBody JieshouDto jieshouDto,
														@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														HttpServletRequest req) {
		//获取登录用户的id
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String id = sysUser.getId();
		String zhidingbiaoid = jieshouDto.getZhidingbiaoid();
		Page<Shenhefankuibiao> page = new Page<Shenhefankuibiao>(pageNo, pageSize);
		QueryWrapper<Shenhefankuibiao> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("zerenzhuguanid",id);
		queryWrapper.eq("zhuangtai","进行中");
		queryWrapper.eq("zhidingbiaoid",zhidingbiaoid);
		IPage<Shenhefankuibiao> pageList = shenhefankuibiaoService.page(page, queryWrapper);
		List<Shenhefankuibiao> list = pageList.getRecords();
		IPage<ShenhefankuiDto> ShenhefankuiDto = new Page<>();
		List<ShenhefankuiDto> shenheDto = new ArrayList<>();
		for(int i =0;i<list.size();i++){
			ShenhefankuiDto shenhefankuiDto = new ShenhefankuiDto();
			BeanUtils.copyProperties(list.get(i), shenhefankuiDto);
			shenhefankuiDto.setWanchengshijian(jieshouDto.getWanchengshijian());
			shenheDto.add(shenhefankuiDto);
		}
		ShenhefankuiDto.setRecords(shenheDto);
		return Result.OK(ShenhefankuiDto);
	}

	//审核反馈接收
	 @PostMapping("/receive")
	 public Result<String> receive(@RequestBody ShenhefankuiDto shenhefankuiDto){
		QueryWrapper<Shenhefankuibiao> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id",shenhefankuiDto.getId());
		Shenhefankuibiao shenhefankuibiao = shenhefankuibiaoService.getOne(queryWrapper);
		shenhefankuibiao.setZhuangtai(shenhefankuiDto.getZhuangtai());
		shenhefankuibiaoService.updateById(shenhefankuibiao);
		return Result.OK("接收成功！");
	 }


	 //查看部门配合处理情况
	 /**
	  * JeecgBoot 的 @Dict 注解不会自动生效，只有在以下两种情况下才会生效：
	  *
	  * 类上加了 @Excel 或继承了 JeecgEntity 基类（并被 Jeecg 提供的 QueryGenerator 或 PageList 机制处理）；
	  *
	  * 通过 Jeecg 的 PageList 或 queryPageList 接口返回的 Result<IPage<T>> 中调用了字典翻译方法（例如 IPage<JSONObject> 被 DictAspect 拦截并翻译）。
	  *
	  * 如果你是自己写的接口 getChuliqingkuang（很可能是自定义 SQL 查询或返回自定义 DTO），那就不会触发字典翻译机制。
	  * **/
	 @PostMapping(value="/chuliqingkuang")
	 public Result<IPage<PeihebumenDto>> chuliqingkuang(@RequestBody JieshouDto jieshouDto){
		 String id = jieshouDto.getZhidingbiaoid();
		 Page<Peihebumenbiao> page = new Page<>();
		 QueryWrapper<Peihebumenbiao> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("zhidingbiaoid",id);
		 IPage<Peihebumenbiao> pageList = peihebumenbiaoService.page(page, queryWrapper);
		 List<Peihebumenbiao> list = pageList.getRecords();
		 IPage<PeihebumenDto> pageDto = new Page<>();
		 List<PeihebumenDto> list1 = new ArrayList<>();
		 for (Peihebumenbiao item : list) {
			 PeihebumenDto dto = new PeihebumenDto();
			 BeanUtils.copyProperties(item, dto);
			 list1.add(dto);
		 }
		 pageDto.setRecords(list1);
		 return Result.OK(pageDto);
	 }

	 /**
	 *   添加
	 *
	 * @param shenhefankuibiao
	 * @return
	 */
	@AutoLog(value = "审核反馈表-添加")
	@Operation(summary="审核反馈表-添加")
	@RequiresPermissions("Shenhefankuibiao:y_shenhefankuibiao:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody Shenhefankuibiao shenhefankuibiao) {
		shenhefankuibiaoService.save(shenhefankuibiao);

		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param shenhefankuibiao
	 * @return
	 */
	@AutoLog(value = "审核反馈表-编辑")
	@Operation(summary="审核反馈表-编辑")
	@RequiresPermissions("Shenhefankuibiao:y_shenhefankuibiao:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Shenhefankuibiao shenhefankuibiao) {
		shenhefankuibiaoService.updateById(shenhefankuibiao);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "审核反馈表-通过id删除")
	@Operation(summary="审核反馈表-通过id删除")
	@RequiresPermissions("Shenhefankuibiao:y_shenhefankuibiao:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		shenhefankuibiaoService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "审核反馈表-批量删除")
	@Operation(summary="审核反馈表-批量删除")
	@RequiresPermissions("Shenhefankuibiao:y_shenhefankuibiao:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.shenhefankuibiaoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "审核反馈表-通过id查询")
	@Operation(summary="审核反馈表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Shenhefankuibiao> queryById(@RequestParam(name="id",required=true) String id) {
		Shenhefankuibiao shenhefankuibiao = shenhefankuibiaoService.getById(id);
		if(shenhefankuibiao==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(shenhefankuibiao);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param shenhefankuibiao
    */
    @RequiresPermissions("Shenhefankuibiao:y_shenhefankuibiao:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Shenhefankuibiao shenhefankuibiao) {
        return super.exportXls(request, shenhefankuibiao, Shenhefankuibiao.class, "审核反馈表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("Shenhefankuibiao:y_shenhefankuibiao:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Shenhefankuibiao.class);
    }

}
